var bookModel = require('../models/books');
var shopCarModel = require('../models/shopCar');
var authorModel = require('../models/author');
var colmunModel = require('../models/colmun');
var index = {
    index: (req, res, next) => {
        colmunModel.find({}).then(doc1 => {
            authorModel.find({}).then(doc2 => {
                bookModel.find({ colmun_id: doc1[0]._id, author_id: doc2[0]._id }).populate('author_id').then(doc3 => {
                    bookModel.find({ colmun_id: doc1[1]._id, author_id: doc2[4]._id }).populate('author_id').then(doc4 => {
                        bookModel.find({ colmun_id: doc1[2]._id, author_id: doc2[8]._id }).populate('author_id').then(doc5 => {
                            bookModel.find({ colmun_id: doc1[3]._id, author_id: doc2[12]._id }).populate('author_id').then(doc6 => {
                                bookModel.find({ colmun_id: doc1[4]._id, author_id: doc2[16]._id }).populate('author_id').then(doc7 => {
                                   
                                    if (req.session.loginUser) {
                                        shopCarModel.find({ userId: req.session.loginUser._id, order_estate: '未完成'}).count().then(doc => {
                                            res.render('index', {
                                                colmun: doc1,
                                                author: doc2,
                                                loginUser: req.session.loginUser,
                                                num: doc,
                                                book: [doc3, doc4, doc5, doc6, doc7]
                                            })
                                        })
                                    } else {
                                        res.render('index', {
                                            colmun: doc1,
                                            author: doc2,
                                            loginUser: req.session.loginUser,
                                            num: 0,
                                            book: [doc3, doc4, doc5, doc6, doc7]
                                        })
                                    }
                                })
                            })
                        })
                    })
                })
            })
        }).catch(err => {
            console.log(err)
        })
    },
    
    bookList:(req,res,next)=>{
        bookModel.find({ author_id: req.params.id}).populate({ path: "author_id"}).then(doc=>{
            res.json({
                status:1,
                result:doc
            })
        }).catch(err=>{
            res.json({
                status: 1,
                msg: err
            })
        })
    },
    headerBookList: (req, res, next) => {
        authorModel.find({colmun:req.body.id}).populate('colmun').then(doc=>{
            res.json({
                status:1,
                result:doc
            })
        })
    }
}
module.exports = index